package com.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import com.entity.UserEntity;
import com.utils.ValidatorUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;
import com.entity.HetongguanliEntity;
import com.entity.view.HetongguanliView;
import com.service.HetongguanliService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;
import com.utils.CommonUtil;

/**
 * 合同管理Controller
 *后端接口
 */
@RestController
@RequestMapping("/hetongguanli")
public class HetongguanliController {
    @Autowired
    private HetongguanliService hetongguanliService;
    
    
            /**
         * 后端列表
         */
                    @RequestMapping("/page")
        public R page(@RequestParam Map<String, Object> params, HetongguanliEntity hetongguanli, HttpServletRequest request) {
                            if (!request.getSession().getAttribute("role").toString().equals("管理员")) {
                    hetongguanli.setUserid((Long) request.getSession().getAttribute("userId"));
                }
                                    EntityWrapper<HetongguanliEntity> ew = new EntityWrapper<HetongguanliEntity>();
            PageUtils page = hetongguanliService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, hetongguanli), params), params));
            request.setAttribute("data", page);
            return R.ok().put("data", page);
        }

        /**
         * 前端列表
         */
                                @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params, HetongguanliEntity hetongguanli, HttpServletRequest request) {



                            hetongguanli.setSfsh("是");
                                    EntityWrapper<HetongguanliEntity> ew = new EntityWrapper<HetongguanliEntity>();
            PageUtils page = hetongguanliService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, hetongguanli), params), params));
            request.setAttribute("data", page);
            return R.ok().put("data", page);
        }

        /**
             * 列表
             */
                    @RequestMapping("/lists")
        public R list( HetongguanliEntity hetongguanli) {
            EntityWrapper<HetongguanliEntity> ew = new EntityWrapper<HetongguanliEntity>();
            ew.allEq(MPUtil.allEQMapPre( hetongguanli, "hetongguanli"));
            return R.ok().put("data", hetongguanliService.selectListView(ew));
        }

        /**
         * 查询
         */
                    @RequestMapping("/query")
        public R query(HetongguanliEntity hetongguanli) {
            EntityWrapper< HetongguanliEntity> ew = new EntityWrapper< HetongguanliEntity>();
            ew.allEq(MPUtil.allEQMapPre( hetongguanli, "Hetongguanli"));
                HetongguanliView hetongguanliView = hetongguanliService.selectView(ew);
            return R.ok("合同管理").put("data", hetongguanliView);
        }

        /**
             * 后端详情
             */
                                @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") Long id) {
                HetongguanliEntity hetongguanli = hetongguanliService.selectById(id);
            return R.ok().put("data", hetongguanli);
        }

        /**
            * 前端详情
            */
                                @RequestMapping("/detail/{id}")
        public R detail(@PathVariable("id") Long id) {
                HetongguanliEntity hetongguanli = hetongguanliService.selectById(id);
                        return R.ok().put("data", hetongguanli);
        }

        /**
        * 后端保存
        */
                    @RequestMapping("/save")
        public R save(@RequestBody HetongguanliEntity hetongguanli, HttpServletRequest request) {
                        hetongguanli.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
            //ValidatorUtils.validateEntity(address);
                            hetongguanli.setUserid((Long) request.getSession().getAttribute("userId"));
                                                            hetongguanliService.insert(hetongguanli);
            return R.ok();
        }

        /**
            * 前端保存
            */
                    @RequestMapping("/add")
        public R add(@RequestBody HetongguanliEntity hetongguanli, HttpServletRequest request) {
                        
                hetongguanli.setUserid((Long) request.getSession().getAttribute("userId"));

                        hetongguanli.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
            //ValidatorUtils.validateEntity(yiyuanjianjie);

                hetongguanliService.insert(hetongguanli);
            return R.ok();
        }

        /**
          * 修改
          */
                    @RequestMapping("/update")
        public R update(@RequestBody HetongguanliEntity hetongguanli, HttpServletRequest request) {
            //ValidatorUtils.validateEntity(zaixianbaoming);
                hetongguanliService.updateById(hetongguanli);//全部更新
            return R.ok();
        }

        /**
             * 删除
             */
                    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids) {
                hetongguanliService.deleteBatchIds(Arrays.asList(ids));
            return R.ok();
        }
    

    
    
}
